package org.pet.controller;

import org.hibernate.validator.constraints.URL;
import org.pet.entity.PetScience;
import org.pet.service.PetScienceService;
import org.pet.util.PageBean;
import org.pet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/petScience")
public class PetScienceController {

    @Autowired
    private PetScienceService petScienceService;

    /**
     * 返回分页的数据列
     *
     * @param pageNum    当前页码
     * @param pageSize   分页大小
     * @param petVariety 根据宠物种类筛选（可以没有）
     * @param state      根据文章类型筛选（可以没有）
     * @return
     */
    @GetMapping("/listPage")
    public Result<PageBean<PetScience>> listPage(@RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize,
                                                 @RequestParam(required = false) String petVariety,
                                                 @RequestParam(required = false) String state) {
        PageBean<PetScience> list = petScienceService.listPage(pageNum, pageSize, petVariety, state);
        return Result.success(list);
    }

    /**
     * 根据文章id获取文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public Result info(Integer id) {

        return petScienceService.info(id);
    }
    /**
     * 新增科普文章
     *
     * @param petScience
     * @return
     */
    @PutMapping("/add")
    public Result add(@RequestBody @Validated PetScience petScience) {

        return petScienceService.add(petScience);
    }

    /**
     * 更新科普文章
     *
     * @param petScience 可以更新title、text、pet_variety、pet_subVariety、pic这几个字段
     * @return
     */
    @PatchMapping("/update")
    public Result update(@RequestBody PetScience petScience) {

        return petScienceService.update(petScience);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {

        return petScienceService.delete(id);
    }
}